package com.neo.web;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neo.common.result.Result;
import com.neo.config.redis.RedisUtil;
import com.neo.config.shiro.realm.MyShiroRealm;
import com.neo.entity.user.SysUser;
import com.neo.entity.user.User2;
import com.neo.service.TestMybatis;

/**
 * 任务管理
 * @author lance
 */
@Controller
@RequestMapping("/nginx")
public class NginxController {
	
	@Autowired
	TestMybatis testMybatis;
	@Autowired
	RedisUtil redisUtil;
	@Autowired
	MyShiroRealm myshiroRealm;
	@RequestMapping("/login")
	@ResponseBody
	public User2 info(){
		System.out.println("ssssshshshshshsh");
		User2 user2 = new User2();
		user2.setId(1);
		user2.setName("哈哈哈");
		user2.setPassword(666666);
		return user2;
	}
	
	@RequestMapping("/loginCheck")
	public String loginCheck(SysUser sysuser){
		System.out.println("开始登录！"+sysuser.getNickname()+"-------"+ sysuser.getPswd());
		UsernamePasswordToken token =null ;
		try {
			 token = new UsernamePasswordToken(sysuser.getNickname(), sysuser.getPswd(),true);
			SecurityUtils.getSubject().login(token);
			
			System.out.println("message+++++登录成功");

		} catch (Exception e) {
			token.clear();
			System.out.println("fail+++++登录失败");
		}
		return "redirect:add";
	}
	
	
	@RequestMapping("/add")
	public String add(){
		return "index";
	}
	
	@RequestMapping("/test")
	public String test(){
		return "index";
	}
	
	@RequestMapping("/test1")
	public String test1(){
		return "index2";
	}
	
	@RequestMapping("/test2")
	@ResponseBody
	public String test2(){
		SecurityUtils.getSubject().logout();
		return "33333";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(){
		//myshiroRealm.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
		//myshiroRealm.clearAllCachedAuthorizationInfo();
		//myshiroRealm.clearAllCache();
		
	
		redisUtil.remove("getAll");
		return "222";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Result getAll(){
		return testMybatis.getAll();
	}
}
